package com.sanpu.kxe.module.dialog.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.InviteMakeMoneyOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.QRCode;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：邀请赚钱适配
 * Created by 薛勇军 on 2019/9/26.
 */

public class ShareEarningsAdapter extends RecyclerView.Adapter<ShareEarningsAdapter.ViewHolder> {

    private Context mContext;

    private List<InviteMakeMoneyOutput> mInviteFriendsOutputs = new ArrayList<>();

    public ShareEarningsAdapter(Context mContext, List<InviteMakeMoneyOutput> mInviteFriendsOutputs) {
        this.mContext = mContext;
        this.mInviteFriendsOutputs = mInviteFriendsOutputs;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_share_earnings_view, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mInviteFriendsOutputs != null && mInviteFriendsOutputs.size()>position) {
            InviteMakeMoneyOutput output = mInviteFriendsOutputs.get(position);
            if (output != null) {
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + output.getImagePath(), holder.bigIv);
                if (!TextUtils.isEmpty(output.getInvitationLinkUrl())) {
                    holder.showLl.setVisibility(View.VISIBLE);
                    String mCodeUrl = output.getInvitationLinkUrl();
                    Bitmap bitmap = QRCode.createQRCode(mCodeUrl, 500);
                    if (bitmap != null) {
                        holder.qrcodeIv.setImageBitmap(QRCode.resizeImage(bitmap, ScreenUtil.dp2px(30), ScreenUtil.dp2px(30)));
                    }
                } else {
                    holder.showLl.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mInviteFriendsOutputs != null)
            return mInviteFriendsOutputs.size();
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout showLl;

        private FrameLayout shareViewFl;

        private ImageView  bigIv, qrcodeIv;

        ViewHolder(View itemView) {
            super(itemView);
            bigIv = ButterKnife.findById(itemView, R.id.bigIv);
            showLl = ButterKnife.findById(itemView, R.id.showLl);
            qrcodeIv = ButterKnife.findById(itemView, R.id.qrcodeIv);
            shareViewFl = ButterKnife.findById(itemView, R.id.shareViewFl);
        }
    }
}
